import requests
import time
from bs4 import BeautifulSoup
import json


def get_zhihu_hot():
    url = "https://www.zhihu.com/billboard"
    resp = requests.get(url)
    resp.encoding = 'utf-8'
    html = resp.text
    soup = BeautifulSoup(html, 'html.parser')
    news = soup.findAll(class_='HotList-itemTitle')
    # print(len(news))
    news_ls = []
    title_ls = []
    for new in news:
        title = new.text
        # print(title)
        title_ls.append(title)
    js_text_dict = json.loads(soup.find('script', {'id': 'js-initialData'}).get_text())
    # print(js_text_dict['initialState']['topstory']['hotList'])
    js_text_dict = js_text_dict['initialState']['topstory']['hotList']
    url_ls = []
    for new in js_text_dict:
        url = new['target']['link']['url']
        url_ls.append(url)

    news_ls = [{'title': title_ls[i], 'url': url_ls[i]} for i in range(len(title_ls))]
    news_ls.reverse()
    return news_ls
